/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.trainee.aplikasipenjualan.ui.servlet;

import com.trainee.aplikasipenjualan.entity.Barang;
import com.trainee.aplikasipenjualan.service.AplikasipenjualanService;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author Jaka
 */ 
 @SuppressWarnings("serial")
 public class EditBarang extends HttpServlet {

    private AplikasipenjualanService service;
    private Barang barang;

    @Override
    public void init(ServletConfig config) throws ServletException {
    ApplicationContext appCtx = WebApplicationContextUtils
    .getWebApplicationContext(config.getServletContext());

    service = (AplikasipenjualanService) appCtx.getBean("aplikasipenjualanService");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {

    resp.setContentType("text/html");
    String id = req.getParameter("id");

    String output = "<html>";
    output += "<head>";
    output += "<title>Daftar Barang</title>";
    output += "</head>";
    output += "<body>";
    output += "<h1>Edit barang</h1>";

    output += "<form method='post' action='simpan.php'>";
    output += "<table>";
    output += "<tr>";
    output += "<td>Kode</td>";
    output += "<td><input type='text' name='kode'value="+id+" readonly></td>";
    output += "</tr>";

    output += "<tr>";
    output += "<td>Nama</td>";
    output += "<td><input type='text' name='nama'></td>";
    output += "</tr>";

    output += "<tr>";
    output += "<td>&nbsp;</td>";
    output += "<td><input type='submit' value='Simpan'></td>";
    output += "</tr>";


    output += "</table>";
    output += "</form>";

    output += "</body>";
    output += "</html>";

    resp.getWriter().print(output);
    resp.getWriter().close();

    }



    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {

    String kode = req.getParameter("kode");
    String nama = req.getParameter("nama");

    Barang b = new Barang();
    b.setKodeBrg(kode);
    b.setNamaBrg(nama);

    service.saveBarang(b);
    resp.sendRedirect("list.php");
    }
    
}
